/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import bols.BOL;
import bols.BOL2;
import bols.BOLLogin;
import bols.BOLOrder;
import entity.Order2;

import entity.Product;
import entity.Product2;
import entity.User2;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author lab1
 */
@ManagedBean
@SessionScoped
public class BeanProduct {

    /** Creates a new instance of BeanProduct */
    public BeanProduct() {
    }
    Product2 pro1;
    //de luu product
    private List<Product2> listprouct;
    //goi bolProduct
    BOL2 bol2 = new BOL2();
    //thao tac voi Cart
    BOL bol = new BOL();
    //de luu Cart
    List<Product> listCart;
    //luu tong tien cua Cart
    private Double totalAmount;
    ////////Phan Login
    private User2 us = new User2();
    //goi bol cua Login
    BOLLogin bollgin = new BOLLogin();
    //check login
    private boolean check;
    //Messager validate
    private String validateCartnull;

    public String getValidateCartnull() {
        return validateCartnull;
    }

    public void setValidateCartnull(String validateCartnull) {
        this.validateCartnull = validateCartnull;
    }

    public List<Product2> getListprouct() {
        listprouct = bol2.showAllProduct();
        return listprouct;
    }

    public Product2 getPro1() {
        return pro1;
    }

    public void setPro1(Product2 pro1) {
        this.pro1 = pro1;
    }
    //Them Product vao Cart

    public String addCart1(Product2 pro2) {
        pro1 = pro2;
        bol.addCart(pro2.getProductID(), pro2.getProductName(), pro2.getPrice(), 1);
        return null;
    }
    //Show Cart

    public String showCart() {
        listCart = bol.showall();
        validateCartnull = "";
        return "show";
    }

    public List<Product> getListCart() {
        return listCart;
    }

    //lay ra Tong tien cua Cart
    public Double getTotalAmount() {
        totalAmount = bol.getTotalAmout();
        return totalAmount;
    }

    //Delete product in Cart
    public void removePro(String proID) {

        bol.removeProduct(proID);
        listCart = bol.showall();
    }

    //dung de edit Cart
    public String editAction(Product pro) {
        pro.setEditPro(true);
        return null;
    }

    //update thay doi cua Cart
    public String saveChange() {
        for (Product item : listCart) {
            item.setEditPro(false);
        }
        bol.updateProduct((ArrayList<Product>) listCart);
        return null;
    }

    //phuong thuc check out cua Cart
    public String CheckOut(Product pro) {
        if (listCart == null) {
            listCart = new ArrayList<Product>();
        }

        BOLOrder orbol = new BOLOrder();
        String dieuthuong = "";
        Order2 or = new Order2();
//        or.s
        String username = bol.showUser().getUserName();
        if (listCart.size() > 0) {
            if (username != null) {
                dieuthuong = "success";
                for (Product item : listCart) {
                    or.setProduct2(new Product2(item.getProductID()));
                    or.setUser2(new User2(username));
                    or.setQuantity(item.getQuantity());
                    orbol.insertOrder2(or);
                }
                bol.updateProduct(new ArrayList<Product>());

            } else {
                dieuthuong = "login";
            }
        } else {
            validateCartnull = "Cart Null";
            dieuthuong = null;
        }
        return dieuthuong;
    }
    //phuong thuc deleteAll Cart

    public void DeleteAllCart() {
        bol.updateProduct(new ArrayList<Product>());
        listCart = bol.showall();
    }

    // check Login xem co duoc hay khong 
    public String login() {
        if (bol.showUser().getUserName() != null) {
            return null;
        }
        return "login";
    }

    public boolean isCheck() {
        return check;
    }

    public User2 getUs() {
        return us;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }
    //phuong thuc check login

    public String checkLogin() {

        check = bollgin.checkLogin(us);
        if (check) {
            bol.updateUser(us);
        }
        validateCartnull = "";
        return check ? "home" : "fails";
    }
    //phuong thuc xem chi tiet Product khi click vao picture

    public String productDetai(String proID) {
        return null;
    }
    //phuong thuc Logout

    public String checkLogOut() {
        bol.updateUser(new User2());
        us=new User2();
        return null;
    }
}
